package us.lithub.ui.activity;

import us.lithub.R;
import us.lithub.Util;
import us.lithub.data.WebConnector;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * The Class Register takes care of the registration and login
 */
public class Register extends Activity {
	
	private static final String TAG = Register.class.getSimpleName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "Sarting registration activity");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
	}
	
	/**
	 * Invoked when the user presses the okay button
	 */
	public void okay(View view) {
		EditText edtUsername = (EditText) findViewById(R.id.edtUsername);
		EditText edtPassword = (EditText) findViewById(R.id.edtPassword);
		
		String username = edtUsername.getText().toString();
		String password = edtPassword.getText().toString();
		
		if (username.trim().length() == 0) {
			Toast.makeText(this, R.string.toast_username_empty, Toast.LENGTH_SHORT).show();
		} else if (password.length() == 0) {
			Toast.makeText(this, R.string.toast_password_empty, Toast.LENGTH_SHORT).show();
		}
		
		login(username, password);
	}
	
	/**
	 * Invoked when the user presses the cancel button
	 */
	public void cancel(View view) {
		finish();
	}

	private void login(String username, String password) {
		WebConnector wc = WebConnector.getInstance();
		int userID = wc.login(username, password);
		Util.setLoginInfo(this, userID, username, password);
		finish();
	}
}